<!DOCTYPE html>
<html>

<head>
    <title>ScalarField / Geotiff WCS </title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
    <link rel="stylesheet" href="examples.css" />

    <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
</head>

<body>
    <h1 class="title mapTitle">ScalarField WCS</h1>
    <div id="map"></div>

    <!-- CDN -->
    <script src="//d3js.org/d3.v4.min.js"></script>
    <script src="//npmcdn.com/leaflet@1.2.0/dist/leaflet.js"></script>
    <script src="//npmcdn.com/geotiff@0.3.6/dist/geotiff.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js"></script>

    <!-- Plugin -->
    <script src="dist/leaflet.canvaslayer.field.js"></script>

    <script>
        let map = L.map('map');

        /* Dark basemap */
        let url = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_nolabels/{z}/{x}/{y}.png';
        L.tileLayer(url, {
            attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/attributions">CARTO</a>',
            subdomains: 'abcd'
        }).addTo(map);

        /* DTM from an OGC-WCS */
        // let getCapabilities = "http://www.juntadeandalucia.es/medioambiente/mapwms/REDIAM_WCS_mdt?SERVICE=wcs&VERSION=1.0.0&REQUEST=GetCapabilities";

        let getCoverage = "https://www.juntadeandalucia.es/medioambiente/mapwms/REDIAM_WCS_mdt?";
        getCoverage += "SERVICE=wcs&";
        getCoverage += "VERSION=1.0.0&";
        getCoverage += "REQUEST=GetCoverage&";
        getCoverage += "COVERAGE=Modelo Digital de Elevaciones (MDE) de Andalucia 100 m&";
        getCoverage += "CRS=EPSG:4326&";
        getCoverage += "BBOX=-7.58390693285438,35.964208075556,-1.6006002035338,38.7503547383259&"; // full extension...
        getCoverage += "RESX=0.01&"; // ...but reduced resolution (~800m)
        getCoverage += "RESY=0.01&";
        getCoverage += "FORMAT=GTiff";

        let urlCoverage = encodeURI(getCoverage);
        //console.log(urlCoverage);

        let geo;
        let dtm;
        let colorScale;

        // palette from http://www.juntadeandalucia.es/medioambiente/site/rediam/menuitem.04dc44281e5d53cf8ca78ca731525ea0/?vgnextoid=f6284d9721fa3410VgnVCM2000000624e50aRCRD&vgnextchannel=8ca090a63670f210VgnVCM2000000624e50aRCRD&vgnextfmt=rediam&lr=lang_es
        let juntaColors = ['#E4EF75', '#B3CF82', '#799B3C', '#60632C', '#A88446', '#CDB2B9', '#593815', '#CDB2B9', '#F4F2F5'];

        d3.request(urlCoverage).responseType('arraybuffer').get(
            function (error, tiffData) {
                // band 0
                geo = L.ScalarField.fromGeoTIFF(tiffData.response);

                colorScale = chroma.scale('RdYlGn').domain([geo.range[1], geo.range[0]]);

                dtm = L.canvasLayer.scalarField(geo, {
                    color: colorScale,
                    opacity: 0.7,
                    inFilter: (v) => v > 0
                }).addTo(map);

                map.fitBounds(dtm.getBounds());

                dtm.on('click', function (e) {
                    if (e.value !== null) {
                        let v = e.value.toFixed(0);
                        let html = (`<span class="popupText">Height: ${v} meters</span><br/>`);
                        html += "<a href='#' onclick='styleGradient();'>gradient</a> | ";
                        html += "<a href='#' onclick='styleClasses();'>classes</a> | ";
                        html += "<a href='#' onclick='original();'>redGreen</a>";
                        let popup = L.popup()
                            .setLatLng(e.latlng)
                            .setContent(html)
                            .openOn(map);
                    }
                });
            });

        function original() {
            dtm.setColor(colorScale);
        }

        function styleClasses() {
            dtm.setColor(chroma.scale(juntaColors).classes([0, 200, 400, 600, 900, 1100, 1400, 1700, 2300, 3500]));
        }

        function styleGradient() {
            dtm.setColor(chroma.scale(juntaColors).domain(geo.range));
        }
    </script>
</body>

</html>